package cn.offcn.service.Impl;

import cn.offcn.entity.Dept;
import cn.offcn.entity.DeptExample;
import cn.offcn.mapper.DeptMapper;
import cn.offcn.service.DaptService;
import cn.offcn.utils.OAResult;
import cn.offcn.utils.TableVo;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author yuliantao
 * @create 2020 -09 -2020-09-21 19:16
 * @description 功能描述
 */
@Service
public class DaptServiceImpl implements DaptService{

    @Autowired
    private DeptMapper deptMapper;
    @Override
    public OAResult addDeprt(Dept dept) {

        int insert = deptMapper.insert(dept);
        if (insert==1){
            return OAResult.ok(200,"添加陈工");
        }
        return OAResult.ok(400,"添加失败");
    }

    @Override
    public TableVo<Dept> getDepts(int page, int limit) {

        PageHelper.startPage(page,limit);
        List<Dept> deptList= deptMapper.selectByExample(new DeptExample());
        PageInfo<Dept> deptPageInfo = new PageInfo<>(deptList);
        TableVo<Dept> tableVo =new TableVo();
        tableVo.setCount(deptPageInfo.getTotal());
        tableVo.setData(deptPageInfo.getList());

        return tableVo;
    }

    @Override
    public OAResult deleteDept(int[] deptnos) {
        if (deptnos.length>0&&deptnos!=null){
            for (int deptno :deptnos){
                deptMapper.deleteByPrimaryKey(deptno);
            }
            return OAResult.ok(200,"");
        }
        return OAResult.ok(400,"操作失败");


    }

    @Override
    public OAResult updateDept(Dept dept) {
        int i = deptMapper.updateByPrimaryKey(dept);
        if (i==1){
        return    OAResult.ok(200,"修改成功");
        }
        return  OAResult.ok(400,"修改失败");
    }

    @Override
    public Dept getDeptByDeptno(int deptno) {
        return deptMapper.selectByPrimaryKey(deptno);


    }
}
